package com.qingke.controller;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingke.annotation.SkipSecurityCheck;
import com.qingke.pojo.Employee;
import com.qingke.pojo.EmployeeAccount;
import com.qingke.pojo.FeedbackRecord;
import com.qingke.pojo.GraduationRecord;
import com.qingke.pojo.Student;
import com.qingke.service.EmployeeService;
import com.qingke.service.FeedbackRecordService;
import com.qingke.service.GraduationRecordService;
import com.qingke.service.StudentService;

@Controller
@RequestMapping("/investigator")
public class InvestigatorController {
	private static Logger logger = Logger.getLogger(InvestigatorController.class);
	private Map<String, Object> map;
	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private FeedbackRecordService feedbackRecordService;
	@Autowired
	private GraduationRecordService graduationRecordService;

	@RequestMapping("getInvestigator")
	@SkipSecurityCheck
	@ResponseBody
	public Map getInvestigator(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		HttpSession se = request.getSession();
		Employee investigator = (Employee) se.getAttribute("user");
		logger.info("get investigator:" + investigator);
		map.put("investigator", investigator);
		return map;
	}

	@RequestMapping("updateInvestigatorMessage")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateInvestigatorMessage(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		HttpSession se = request.getSession();
		Employee investigator = (Employee) se.getAttribute("user");
		String userId = request.getParameter("user_id");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		logger.info("get InvestigatorMessage[" + "userId=" + userId + ",email=" + email + ",phone=" + phone + "]");
		if (!userId.equals(investigator.getEmployeeNumber())) {
			map.put("isSame", "no");
		} else {
			investigator.setEmail(email);
			investigator.setPhone(phone);
			int count = employeeService.updateEmployeeByPrimaryKey(investigator);
			if (count > 0) {
				map.put("update", "success");
			} else {
				map.put("update", "failed");
			}
		}
		Collection<Object> c = map.values();
		Iterator it = c.iterator();
		for (; it.hasNext();) {
			System.out.println(it.next());
		}
		return map;
	}

	@RequestMapping("updateInvestigatorPassword")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateInvestigatorPassword(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String oldPassword = request.getParameter("old_password");
		String newPassword = request.getParameter("new_password");
		String confrimPassword = request.getParameter("confirm_password");
		logger.info("get InvestigatorPassword:old_password" + oldPassword + ",new_password=" + newPassword
				+ ",confirm_password=" + confrimPassword);
		HttpSession se = request.getSession();
		Employee investigator = (Employee) se.getAttribute("user");
		EmployeeAccount employeeAccount = employeeService.getEmployeeAccountById(investigator.getId());
		if (!oldPassword.equals(employeeAccount.getPassword())) {
			map.put("isExsit", "false");
		} else if (!newPassword.equals(confrimPassword)) {
			map.put("equal", "false");
		} else {
			employeeAccount.setPassword(newPassword);
			int count = employeeService.updateEmployeeAccountByPrimaryKey(employeeAccount);
			if (count > 0) {
				map.put("update", "success");
			} else {
				map.put("update", "false");
			}
		}
		Collection<Object> c = map.values();
		Iterator it = c.iterator();
		for (; it.hasNext();) {
			System.out.println(it.next());
		}
		return map;
	}

	@RequestMapping("listFeedbackRecord")
	@SkipSecurityCheck
	@ResponseBody
	public Map listFeedbackRecord(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		List<FeedbackRecord> feedbackRecord = new ArrayList();
		List<FeedbackRecord> feedbackRecords = feedbackRecordService.listFeedbackRecord();
		if (!feedbackRecords.isEmpty()) {
			for (FeedbackRecord fr : feedbackRecords) {
				logger.info("list feedbackRecord:" + fr);
				if (!fr.getIsDeleted()) {
					feedbackRecord.add(fr);
				}
			}
			map.put("feedbackRecord", feedbackRecord);
		} else {
			map.put("feedbackRecord", "");
		}
		return map;
	}

	@RequestMapping("deleteRecord")
	@SkipSecurityCheck
	@ResponseBody
	public Map deleteRecord(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		int id = Integer.valueOf(request.getParameter("id"));
		logger.info("get feedback_id=" + id);
		int count = feedbackRecordService.softDelete(id);
		if (count > 0) {
			map.put("isDelete", "success");
		} else {
			map.put("isDelete", "failed");
		}
		return map;
	}

	@RequestMapping("createRecord")
	@SkipSecurityCheck
	@ResponseBody
	public Map createRecord(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String student_name = request.getParameter("student_name");
		String time = request.getParameter("time");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long lt = new Long(time);
		Date d = new Date(lt);
		String res = format.format(d);
		Date date = null;
		try {
			date = format.parse(res);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String type = request.getParameter("type");
		String content = request.getParameter("content");
		logger.info("get studentName=" + student_name + ",date=" + date + ",type=" + type + ",content=" + content);
		Student student = studentService.getStudentByStudentName(student_name);
		if (student == null) {
			map.put("isExist", "no");
			System.out.println(student == null);
		} else {
			FeedbackRecord feedbackRecord = new FeedbackRecord();
			feedbackRecord.setStudentId(student.getId());
			feedbackRecord.setDate(date);
			feedbackRecord.setFeedbackType(type);
			feedbackRecord.setContent(content);
			feedbackRecord.setIsDeleted(false);
			int count = feedbackRecordService.saveFeedbackRecord(feedbackRecord);
			if (count > 0) {
				map.put("feedback", "success");
			} else {
				map.put("feedback", "failed");
			}
		}
		return map;
	}

	@RequestMapping("listGraduationRecord")
	@SkipSecurityCheck
	@ResponseBody
	public Map listGraduationRecord(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		List<GraduationRecord> graduationRecord = new ArrayList();
		List<GraduationRecord> graduationRecords = graduationRecordService.listGraduationRecord();
		if (!graduationRecords.isEmpty()) {
			for (GraduationRecord gr : graduationRecords) {
				if (!gr.getIsDeleted()) {
					graduationRecord.add(gr);
				}
			}
			map.put("graduationRecord", graduationRecord);
		} else {
			map.put("graduationRecord", "");
		}
		return map;
	}

	@RequestMapping("getGraduationRecord")
	@SkipSecurityCheck
	@ResponseBody
	public Map getGradutionRecord(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		HttpSession se = request.getSession();
		int id = Integer.valueOf(request.getParameter("id"));
		logger.info("get graudtionRecord_id=" + id);
		GraduationRecord graduationRecord = graduationRecordService.getGraduationRecordById(id);
		se.setAttribute("graduationRecord", graduationRecord);
		map.put("graduationRecord", graduationRecord);
		return map;
	}

	@RequestMapping("updateGraduationRecord")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateGraduationRecord(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		HttpSession se = request.getSession();
		Employee investigator = (Employee) se.getAttribute("user");
		GraduationRecord graduationRecord = (GraduationRecord) se.getAttribute("graduationRecord");
		logger.info("get graduationRecord=" + graduationRecord);
		String student_name = request.getParameter("student_name");
		int salary = Integer.valueOf(request.getParameter("salary"));
		String company = request.getParameter("company");
		String note = request.getParameter("note");
		Date date = new Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		logger.info("get studentName=" + student_name + ",salary=" + salary + ",company=" + company + ",note=" + note
				+ ",date=" + date);
		Student student = studentService.getStudentByStudentName(student_name);
		graduationRecord.setCompany(company);
		graduationRecord.setCreateTime(timestamp);
		graduationRecord.setEmployeeId(investigator.getId());
		graduationRecord.setStudentId(student.getId());
		graduationRecord.setNote(note);
		graduationRecord.setSalary(salary);
		int count = graduationRecordService.upadteGraduationRecord(graduationRecord);
		if (count > 0) {
			map.put("update", "success");
		} else {
			map.put("update", "failed");
		}
		return map;
	}

	@RequestMapping("createGraduationRecord")
	@SkipSecurityCheck
	@ResponseBody
	public Map createGraduationRecord(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		HttpSession se = request.getSession();
		Employee investigator = (Employee) se.getAttribute("user");
		String student_name = request.getParameter("student_name");
		int salary = Integer.valueOf(request.getParameter("salary"));
		String company = request.getParameter("company");
		String note = request.getParameter("note");
		Date date = new Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		logger.info("get studentName=" + student_name + ",salary=" + salary + ",company=" + company + ",note=" + note
				+ ",date=" + date);
		Student student = studentService.getStudentByStudentName(student_name);
		GraduationRecord graduationRecord = new GraduationRecord();
		graduationRecord.setCompany(company);
		graduationRecord.setCreateTime(timestamp);
		graduationRecord.setEmployeeId(investigator.getId());
		graduationRecord.setStudentId(student.getId());
		graduationRecord.setNote(note);
		graduationRecord.setIsDeleted(false);
		graduationRecord.setSalary(salary);
		int count = graduationRecordService.saveGraduationRecord(graduationRecord);
		if (count > 0) {
			map.put("create", "success");
		} else {
			map.put("create", "failed");
		}
		return map;
	}

}
